package LinkList;

/**
 * 队列，先进先出
 *
 * @author Delouch
 * @date 2024-10-17 16:40:04
 */

public class Queue<E> {

    private SingleLinkNode<E> top;

    private int size;

    /**
     * TODO 入队
     * @param element 入队元素
     * @return void
     * @author Delouch
     * @date 10/17/24
     */
    public void pop(E element) {
        SingleLinkNode node = new SingleLinkNode(element, null);
        if (top == null) {
            top = node;
        }else {
            SingleLinkNode last = this.top;
            while (last.nextNode != null) {
                last = last.nextNode;
            }
            last.nextNode = node;
        }
        this.size += 1;
    }

    /**
     * TODO 出队
     * @return E 出队元素
     * @author Delouch
     * @date 10/17/24
     */
    public E push() {
        if (top == null) {
            return null;
        }
        this.size -= 1;
        E pushValue = this.top.value;
        this.top = this.top.nextNode;
        return pushValue;
    }

    /**
     * TODO 获取队头元素
     * @return E    队头元素
     * @author Delouch
     * @date 10/17/24
     */
    public E top() {
        if (this.top == null) {
            return null;
        }
        return this.top.value;
    }

    public int size() {
        return this.size;
    }

    public boolean isEmpty() {
        return this.size > 0 ? false : true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        SingleLinkNode node = this.top;
        while (node != null) {
            sb.append(node.value).append("  --  ");
            node = node.nextNode;
        }
        return sb.append("\n").toString();
    }

}
